package com.own.business.wechat.mobile.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.own.business.wechat.mobile.entity.bo.WechatMobileAuthenticationBo;
import com.own.business.wechat.mobile.entity.map.WechatMobileAuthenticationMapVo;
import com.own.business.wechat.mobile.entity.po.WechatMobileAuthentication;
import com.own.business.wechat.mobile.entity.query.WechatMobileAuthenticationQuery;
import com.own.business.wechat.mobile.entity.vo.WechatMobileAuthenticationVo;
import com.own.business.wechat.mobile.mapper.WechatMobileAuthenticationMapper;
import com.own.business.wechat.mobile.service.WechatMobileAuthenticationService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * WechatMobileAuthenticationServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-30 09:06:33
 */
@Slf4j
@Service
public class WechatMobileAuthenticationServiceImpl extends AbstractBaseService<
        WechatMobileAuthentication,
        WechatMobileAuthenticationBo,
        WechatMobileAuthenticationVo,
        WechatMobileAuthenticationMapVo,
        WechatMobileAuthenticationQuery,
        WechatMobileAuthenticationMapper
        > implements WechatMobileAuthenticationService {

    @Resource
    private WechatMobileAuthenticationMapper wechatMobileAuthenticationMapper;

    /**
     * 保存微信认证信息
     *
     * @param code       原始code
     * @param jsonObject 微信认证信息
     * @return 认证信息
     */
    @Override
    public WechatMobileAuthentication saveOfJson(String code, JSONObject jsonObject) {
        var po = new WechatMobileAuthentication();
        po.setScope(jsonObject.getString("scope"));
        po.setOpenId(jsonObject.getString("openid"));
        po.setUnionId(jsonObject.getString("unionid"));
        po.setAccessToken(jsonObject.getString("access_token"));
        po.setRefreshToken(jsonObject.getString("refresh_token"));
        po.setExpiresIn(jsonObject.getInteger("expires_in"));
        po.setCode(code);
        wechatMobileAuthenticationMapper.insert(po);
        return po;
    }
}
